package com.wc.wcWebsite.controller;


import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wc.wcWebsite_api.UserService;
import com.wc.wcWebsite_api.WeatherService;
import com.wc.wcWebsite_model.CommonUtil;
import com.wc.wcWebsite_model.CookieUtil;
import com.wc.wcWebsite_model.User;
import com.wc.wcWebsite_model.WeatherResponse;


@Controller
public class loginController {
	private final static Logger logger=LoggerFactory.getLogger(loginController.class);
	
	@Autowired
	private UserService userService;
	@Autowired
	private WeatherService weatherService;
	
	@RequestMapping("tologin")
	public String tologin(HttpServletRequest request){
		Cookie ecookie=CookieUtil.getCookieByName(request, "email");
		Cookie pcookie=CookieUtil.getCookieByName(request, "password");
		if(ecookie != null && pcookie != null){
			request.setAttribute("email", ecookie.getValue());
			request.setAttribute("password", pcookie.getValue());
		}
		return "login";
	}
	
	@RequestMapping("main")
	public String main(HttpServletRequest request){
		User user=(User) request.getSession().getAttribute("userInfo");
		request.setAttribute("user", user);
		return "home";
	}
	
	@RequestMapping("test")
	public String test(HttpServletRequest request){
		String name=userService.getName();
		request.setAttribute("name", name);
		return "test";
	}
	
	
	@RequestMapping("logInto")
	@ResponseBody
	public String login(HttpServletRequest request,HttpServletResponse response){
		String email=(String) request.getParameter("usremail");
		String password=(String) request.getParameter("password");
		String keep=(String) request.getParameter("keeplogin");
		Map<String,Object> map=userService.userLogin(email, password);
		if("0000".equals(map.get("retCode"))){
			request.getSession().setAttribute("userInfo", map.get("retObject"));
			if("1".equals(keep)){
				CookieUtil.addCookie(response, "email", email);
				CookieUtil.addCookie(response, "password", password);
			}
			return "00000";
		}else{
			return "00001";
		}
	}
	
	@RequestMapping("signup")
	@ResponseBody
	public String signup(HttpServletRequest request){
		String useremail=(String) request.getParameter("usremail");
		String username=(String) request.getParameter("usrname");
		String password=(String) request.getParameter("password");
		User user=new User();
		user.setEmail(useremail);
		user.setPassword(CommonUtil.EncodeByMD5(password));
		user.setUsername(username);
		if (userService.addUser(user)) {
			return "00000";
		}else{
			return "00001";
		}
	}
	
	@RequestMapping("isExist/{type}")
	@ResponseBody
	public Boolean isExist(HttpServletRequest request,@PathVariable("type")String type){
		User user=new User();
		if("1".equals(type)){
			String email=request.getParameter("email");
			user=userService.findUserByEmail(email);
		}else if("2".equals(type)){
			String name=request.getParameter("username");
			user=userService.findUserByName(name);
		}
		
		if(user != null){
			return false;
		}else{
			return true;
		}
	}
	
	@RequestMapping("weather")
	@ResponseBody
	public WeatherResponse getWeather(){
		WeatherResponse wr=weatherService.getDataByCityName("北京");
		return wr;
	}
	
}
	